﻿using PickingGuide.Business;
using PickingGuide.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PickingGuideForm
{
    public partial class FrmPickingSet : Form
    {
        public Action RefreshUiEvent;
        PickingSetupBLL pickingSetupBLL = new PickingSetupBLL();
        SystemSetupBLL systemSetupBLL = new SystemSetupBLL();

        PickingSetupModel _pickingSetupModel;
        public FrmPickingSet()
        {
            InitializeComponent();
            var systemSetupModel = systemSetupBLL.GetModel();
            List<string> list = new List<string>();
            for (int i = 1; i <= systemSetupModel.PartsQty; i++)
            {
                list.Add($"部品{i}");
            }
            cbPartsNum.DataSource = list;
            cbPartsNum.SelectedIndex = 0;
        }

        public FrmPickingSet(PickingSetupModel model)
        {
            InitializeComponent();
            _pickingSetupModel = model;
            txtName.Text = _pickingSetupModel.Name;
            txtCarName.Text = _pickingSetupModel.ModelName;
            txtCarCode.Text = _pickingSetupModel.CarCode;
            txtCode.Text = _pickingSetupModel.Code;

            var systemSetupModel = systemSetupBLL.GetModel();
            List<string> list = new List<string>();
            for(int i = 1;i <= systemSetupModel.PartsQty; i++)
            {
                list.Add($"部品{i}");
            }
            cbPartsNum.DataSource = list;
            cbPartsNum.SelectedIndex = _pickingSetupModel.PartsNum - 1;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (_pickingSetupModel == null)
            {
                var model = pickingSetupBLL.GetModel(cbPartsNum.SelectedIndex+1,txtCode.Text);
                if (model != null)
                {
                    MessageBox.Show("已存在相同的车型配置，不能重复！");
                    return;
                }
                _pickingSetupModel = new PickingSetupModel();
                _pickingSetupModel.Name = txtName.Text;
                _pickingSetupModel.ModelName = txtCarName.Text;
                _pickingSetupModel.CarCode = txtCarCode.Text;
                _pickingSetupModel.PartsNum = cbPartsNum.SelectedIndex + 1;
                _pickingSetupModel.Code = txtCode.Text;

                var result = pickingSetupBLL.Add(_pickingSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("添加拣货配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("添加拣货配置失败：" + result.Dsca);
            }
            else
            {
                _pickingSetupModel.Name = txtName.Text;
                _pickingSetupModel.ModelName = txtCarName.Text;
                _pickingSetupModel.CarCode = txtCarCode.Text;
                _pickingSetupModel.PartsNum = cbPartsNum.SelectedIndex + 1;
                _pickingSetupModel.Code = txtCode.Text;

                var result = pickingSetupBLL.Update(_pickingSetupModel);
                if (result.IsSuccess)
                {
                    MessageBox.Show("修改拣货配置成功");
                    RefreshUiEvent?.BeginInvoke(null, null);
                    this.Close();
                }
                else
                    MessageBox.Show("修改拣货配置失败：" + result.Dsca);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
